package com.huixin.zhang.common.utils;

import java.util.Random;

public class RandomStringUtil {

    private static final Random RANDOM = new Random();
    private static final char[] ALL_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    private static final char[] ALPHABET_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final char[] NUM_CHAR = "0123456789".toCharArray();

    /**
     * 生成length长度的字符串
     */
    public static final String randomString(int length) {
        return randomString(ALL_CHAR, length);
    }
    public static final String randomString(char[] charArr, int length) {
        StringBuffer str = new StringBuffer(length);
        for (int i = 0; i < length; i++) {
            str.append(charArr[RANDOM.nextInt(charArr.length)]);
        }
        return str.toString();
    }

    public static final String randomString(String type, int length) {
        if ("ALL_CHAR".equals(type)) {
            return randomString(ALL_CHAR, length);
        }

        if ("ALPHABET_CHAR".equals(type)) {
            return randomString(ALPHABET_CHAR, length);
        }

        if ("NUM_CHAR".equals(type)) {
            return randomString(NUM_CHAR, length);
        }

        return "";
    }


}
